<?php
/**
 * @file : celebrity.php
 * @date : 2014-12-26
 * @author : yuebin
 */
namespace haibao\cms\data;

class Interactive extends \haibao\cms\data\BaseMysql{
    
    public function __construct(){
        parent::__construct('\haibao\cms\model\data\InteractiveResource');
    }
	
    public function del($id){
    	$sql = 'delete from InteractiveResource where Id=%s';
    	$this->query($sql,array($id));
    }
    
    public function editContentLink($id,$contentLink){
    	$sql = 'update InteractiveResource set ContentLink=%s where Id=%s';
    	$this->query($sql,array($contentLink,$id));
    }
    
    public function sendInteractive($id,$contentLink){
    	$sql = 'update InteractiveResource set Status='.\haibao\cms\model\data\InteractiveResource::STATUS_SEND.',ContentLink=%s where Id=%s';
    	$this->query($sql,array($contentLink,$id));
    }
    
    public function getInteractiveCount($type,$date){
    	$count = 0;
    	$sql = 'select count(*) as num from InteractiveResource where ResourceType=%s and ResourceDate=%s';
    	$result = $this->query($sql,array($type,$date));
    	while ($result->fetch_assoc()){
    		$count = $result->getData('num');
    	}
    	
    	return $count;
    }
    
    /**
     * 根据排期ID获取操作记录
     * @param unknown $id
     * @return multitype:NULL
     */
    public function getOneInteractiveHistory($id){
        $sql = 'select Id,UserId,OperationContent,CreateTime from InteractiveResourceHistory where InteractiveId = %s';
        $result = $this->query($sql,array($id));
        $interactiveHistoryList = array();
        while($row = $result->fetch_assoc()){
            $interactiveHistoryList[] = $row;
        }
        if($interactiveHistoryList){
            foreach($interactiveHistoryList as $key=>$val){
                 $interactiveHistoryList[$key]['OperationContent'] = json_decode($val['OperationContent']);
            }
        }
        return $interactiveHistoryList;
    }   
    
    public function searchCount($resourceType,$startDate,$endDate){
    	$data = $parameters = array();
    	$sql = 'select ResourceType,count(*) as num from InteractiveResource ';
    	$sql .= ' where Status='.\haibao\cms\model\data\InteractiveResource::STATUS_SEND;
    	
    	if ($resourceType){
    		$str = preg_replace('/(\d+)/', '%s', implode(',', $resourceType));
    		$sql .= ' and ResourceType in ('.$str.')';
    		$parameters = array_merge($parameters,$resourceType);
    	}
    	
    	if ($startDate){
    		$sql .= ' and ResourceDate between %s and %s';
    		$parameters = array_merge($parameters,array($startDate,$endDate));
    	}else{
    		$sql .= ' and ResourceDate <= %s';
    		$parameters = array_merge($parameters,array($endDate));
    	}
    	
    	$sql .= ' group by ResourceType';
    	$result = $this->query($sql,$parameters);
    
    	while ($result->fetch_assoc()){
    		array_push($data,array('ResourceType'=>$result->getData('ResourceType'),'Count'=>$result->getData('num')));
    	}
    	
    	return $data;
    }
}
